using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SharePoint.ImportExport.Core;

namespace CodePlex.ExtractedDataReorganizer
{
    public partial class frmEditList : Form
    {
        List List;
        string DirectoryName;
        // Declare an object variable of the type to be deserialized.
        SharePoint.ImportExport.Core.ListOfItems exportedList = new SharePoint.ImportExport.Core.ListOfItems();
        
        public frmEditList(List list, string directoryName)
        {
            this.List = list;
            DirectoryName = directoryName;
            InitializeComponent();
        }

        private void frmEditList_Load(object sender, EventArgs e)
        {
            txtName.Text = this.List.Name;
            txtTemplate.Text = this.List.Template;
            txtBaseTemplate.Text = this.List.BaseTemplate;
            chkOnQuickLaunch.Checked = this.List.QuickLaunch;
            chkHiddenList.Checked = this.List.Hidden;
            // A FileStream is needed to read the XML document.
            using (FileStream fs = new FileStream(DirectoryName + this.List.RelativeitemXML, FileMode.Open))
            {

                using (XmlReader reader = new XmlTextReader(fs))
                {
                    try
                    {
                        // Create an instance of the XmlSerializer specifying type and namespace.
                        XmlSerializer serializer = new XmlSerializer(typeof(SharePoint.ImportExport.Core.ListOfItems));
                        // Use the Deserialize method to restore the object's state.
                        exportedList = (SharePoint.ImportExport.Core.ListOfItems)serializer.Deserialize(reader);
                        txtNumberOfItems.Text = exportedList.ItemTypes[0].Items.Count.ToString();
                    }
                    catch (Exception Ex)
                    {
                    }
                    finally
                    {
                        reader.Close();
                        fs.Close();
                    }
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.List.Name = txtName.Text;
            if (this.List.Template != txtTemplate.Text)
            {
                foreach (ItemType it in exportedList.ItemTypes)
                {
                    if (this.List.Template == it.Name)
                    {
                        it.Name = txtTemplate.Text;
                    }
                }
                exportedList.Serialize(DirectoryName + this.List.RelativeitemXML);

            }
            this.List.Template = txtTemplate.Text;
            this.List.BaseTemplate = txtBaseTemplate.Text;
            this.List.QuickLaunch = chkOnQuickLaunch.Checked;
            this.List.Hidden = chkHiddenList.Checked;
        }
    }
}